<template>
	<view class='container'>
		<view class='header bg-ff row padding'>
			<icon type='search' size='20' color='#999' />
			<input placeholder='请输入地址' class='col padding-l' @input='bindInput' focus auto-focus @confirm='bindConfirm'></input>
		</view>
		<view class='bg-ff'>
			<view class='padding color-99 font-26'>从下面列表中选择商场</view>
			<view class='padding border-t list position-r' v-for='(item, index) in list' :key='index' @click='address(item)'
			 hover-class="opacity7">
				<view class='padding-b'>
					{{item.marketName}}
					<text v-if="!isBlank(item.address)">({{item.address}})</text>
				</view>
				<view class='color-99 color-99 bottm2line'>
					<text class="left">见面地点: {{item.meetAddress}}</text>
					<text class="right">距您: {{item.distance | distance }}</text>
				</view>
			</view>
			<view class='none_data' v-if='list.length === 0'>
				<view>未搜索到相关商场</view>
			</view>
		</view>

		<u-loadmore :status="status" @loadmore="moreData" />
	</view>

</template>

<script>
	import {
		mapState
	} from 'vuex';
	import {
		isBlank
	} from '@/utils/common.js'

	export default {
		data() {
			return {
				list: [],
				queryForm: {
					marketName: null,
					pageNum: 1,
					pageSize: 10,
				},
				status: 'loadmore',
			}
		},
		filters: {
			distance(val) {
				if(val < 1000)
				    return val.toFixed(0) + "m"
				else if(val > 1000)
					return (Math.round(val/100)/10).toFixed(1) + "km"
			}
		},
		computed: {
			...mapState(['userInfo'])
		},
		onLoad(options) {
			this.searchList('refresh')
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.queryForm = {
				pageNum: 1,
				pageSize: 10
			}
			this.status = "loadmore"
			this.searchList()
		},
		// 加载更多
		onReachBottom() {
			if (this.status === 'loadmore') {
				this.moreData()
			}
		},
		methods: {
			isBlank,
			searchList() {
				this.queryForm.pageNum = 1
				var data = {
					latitude: this.userInfo.latitude,
					longitude: this.userInfo.longitude,
					...this.queryForm
				}

				uni.showLoading({
					title: "正在搜索商场"
				})

				this.$u.api.findMarket(data).then(res => {
					uni.stopPullDownRefresh()
					uni.hideLoading()
					if (res.code === 200) {
						this.list = res.data.records
						this.loadingType = res.data.records.length === 10 ? 'more' : 'nomore';
					}
				})
			},
			// 加载更多
			moreData() {
				this.status = 'loading';
				this.queryForm.pageNum++;

				var data = {
					latitude: this.userInfo.latitude,
					longitude: this.userInfo.longitude,
					...this.queryForm
				}

				this.$u.api.findMarket(data).then(res => {
					if (res.code === 200) {
						this.list = this.list.concat(res.data.records)
						if (this.list.length < res.data.total) {
							this.status = 'loadmore'
						} else {
							this.status = 'nomore'
						}
					}
				})
			},
			bindInput(e) {
				let val = e.detail.value
				this.queryForm.marketName = val
				this.searchList()
			},
			bindConfirm(e) {
				let val = e.detail.value
				this.queryForm.marketName = val
				this.searchList()
			},
			// 选择地址
			address(item) {
				let pages = getCurrentPages();
				let prePage = pages[pages.length - 2];
				console.log(prePage)
				prePage.$vm.marketId = item.id
				prePage.$vm.marketName = item.marketName
				prePage.$vm.jumpPoint(item.latitude, item.longitude)
				
				uni.navigateBack({
					delta: 1,
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #F6F6F6;
		font-size: 32rpx;
	}
	
	.none_data {
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 20upx auto;
		color: #6C6C6C;
	}
	
	.bottm2line {
		display: flex;
		justify-content: space-between;
		align-items: center;
		.left {
			font-size: 28upx;
		}
		.right {
			font-size: 22upx;
		}
	}
	

	.item,
	.item-forward {
		background: #fff;
		padding: 25rpx 90rpx 25rpx 25rpx;
		position: relative;
		line-height: 46rpx;
	}

	.item-forward::before {
		content: '';
		width: 20rpx;
		height: 20rpx;
		border-top: 2px solid #a9a9a9;
		border-right: 2px solid #a9a9a9;
		border-radius: 2px;
		position: absolute;
		top: 50%;
		right: 35rpx;
		transform: rotate(45deg) translateY(-50%);
	}

	.item image,
	.item-forward image {
		width: 46rpx;
		height: 46rpx;
		display: block;
		margin-right: 10rpx;
		position: relative;
	}

	.row,
	.item,
	.item-forward,
	.coupons {
		display: flex;
		width: 100%;
	}

	.row-wrap {
		flex-wrap: wrap;
	}

	.col,
	.coupons .left {
		flex: 1;
		display: block;
		width: 100%;
	}

	.col-center {
		height: 100%;
		display: flex;
		align-items: center;
	}

	.float-r {
		float: right;
	}

	.padding {
		padding: 20rpx 25rpx;
	}

	.padding-t {
		padding-top: 20rpx;
	}

	.padding-b {
		padding-bottom: 20rpx;
	}

	.padding-l {
		padding-left: 25rpx;
	}

	.padding-r {
		padding-right: 25rpx;
	}

	.padding-tb {
		padding-top: 20rpx;
		padding-bottom: 20rpx;
	}

	.padding-lr {
		padding-left: 25rpx;
		padding-right: 25rpx;
	}

	.margin {
		margin: 20rpx 25rpx;
	}

	.margin-t {
		margin-top: 20rpx;
	}

	.margin-b {
		margin-bottom: 20rpx;
	}

	.margin-tb {
		margin-top: 20rpx;
		margin-bottom: 20rpx;
	}

	.margin-lr {
		margin-left: 25rpx;
		margin-right: 25rpx;
	}

	.border,
	.border-t,
	.border-r,
	.border-b,
	.border-l {
		position: relative;
	}

	.border {
		border: .5px solid #eee;
	}

	.border-t::after,
	.border-r::after,
	.border-b::after,
	.border-l::after {
		content: '';
		position: absolute;
		/*background: #eee;*/
		background: linear-gradient(to top, #eee .7px, transparent .7px);
	}

	.border-t::after,
	.border-b::after {
		height: 1px;
		left: 25rpx;
		right: 25rpx;
		top: 0;
	}

	.border-b::after {
		top: auto;
		bottom: 0;
	}

	.border-l::after,
	.border-r::after {
		width: 1px;
		top: 0;
		bottom: 0;
		left: 0;
		background: linear-gradient(to right, #eee .7px, transparent .7px);
	}

	.border-r::after {
		left: auto;
		right: 0;
	}

	.bg {
		background: #E74246;
	}

	.bg-ff {
		background: #fff;
	}

	.color {
		color: #E74246;
	}

	.color-00 {
		color: #000;
	}

	.color-ff {
		color: #fff;
	}

	.color-99,
	.icon_img_tip {
		color: #999;
	}

	.color-6c {
		color: #6c6c6c;
	}

	.text-right {
		text-align: right;
	}

	.font-26 {
		font-size: 26rpx;
	}

	.position-r {
		position: relative;
	}

	.container {
		padding-top: 110rpx;
	}

	.header {
		position: fixed;
		z-index: 999;
		top: 0;
		left: 0;
		border-bottom: 1px solid #eee;
	}
</style>
